#!/bin/bash
function Build(){
	build=$1
	if [ -z ${build} ];then
		build=build
	fi
	if [ ! -d ${build} ];then
		mkdir ${build}
	fi
	printf "===> %s\n" ${build}
	pushd $build
	cmake ..
	make -j16
	popd
}
function Help(){
ARGS=`getopt -o br:t::h:: --long build,run:,test::,help:: -n 'build.sh' -- "$@"`
while true
do
    case "$1" in
        -b|--build) 
            printf "\033[32m[INFO: ] Build project....\n\033[0m";
			Build
            shift
            ;;
        -r|--run)
			if [ -z ${2} ];then
				program=main
			else
				program=${2}
			fi
			if [ ! -z ${program} ];then
				program=`find . -name ${program}`
			fi
			eval '${program}'
            printf "\033[32m[INFO: ]Run Project.... %s\n\033[0m" ${program}
				 
            shift 2
            ;;
        -h|--help) 
            printf "\033[32m[Usage: ] bash build.sh --build|--test\n\033[0mBUILD";
            shift
            ;;
        -t|--test)
            case "$2" in
                "")
                    printf "\033[32m[INFO: ]Current Param is Empty,Exit...\n\033[0m";
                    shift 2  
                    ;;
                *)
                    printf "\033[31m[INFO: ]\033[0m, argument $2";
                    shift 2;
                    ;;
            esac
            ;;
        --)
            shift
            break
            ;;
        *)
            printf "\033[31m[ERROR:] 参数错误,当前参数[%s]\n\033[0m" $@
            exit 1
            ;;
    esac
done
}


#echo $@

#-o或--options选项后面接可接受的短选项，如ab:c::，表示可接受的短选项为-a -b -c，其中-a选项不接参数，-b选项后必须接参数，-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项，用逗号分开，冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
# if [ $? != 0 ]; then
#     printf "\033[31m[ERROR: ]参数获取错误....\n\033[0m"
#     exit 1
# fi

#echo $ARGS
#将规范化后的命令行参数分配至位置参数（$1,$2,...)
# eval set -- "${ARGS}"
# printf "===> %s\n" ${ARGS}
Help $*
